package poo.exo1;

import java.io.*;
import java.util.*;

public class Vendeur {
        private String nom;
        private List<Vehicule> vehiculePossede;
        private HashMap vehiculehm = new HashMap(); 
        
        public Vendeur(String nom) {
                this.nom=nom;
                vehiculePossede = new ArrayList<Vehicule>();
                this.setVehiculePossede(vehiculePossede);
        }

        @Override
        public String toString() {
                return "Vendeur " + nom +"\n Possede : \n"+this.getVehiculehm() ;
        }
        
        public List<Vehicule> getVehiculePossede() {
                return vehiculePossede;
        }



        public void setVehiculePossede(List<Vehicule> vehiculePossede) {
                this.vehiculePossede = vehiculePossede;
                
        }
        
         public boolean addVehiculePossede(Vehicule vehicules) {
                 try {
                        vehicules.majExemplaire();
                } catch (MonException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
         return vehiculePossede.add(vehicules);
         
         }
         
         public void readData(Scanner in)
                {
                        String line = in.nextLine ( ) ;
                        // e x p r e s s i on r a t i o n n e l l e pour par s e r chaque l i g n e s e l on l e se p a r a t eur j
                        String [ ] tokens = line.split ( "\\| " ) ;
                         // l e nom se t rouv e dans l a premiere case du t a b l e a u
                        String nom = tokens [ 0 ] ;
                        if( nom.equals(this.nom))
                        {
                                System.out.println(this.toString()) ;
                        }
                }
                @Override
                public int hashCode() {
                        final int prime = 31;
                        int result = 1;
                        result = prime * result + ((nom == null) ? 0 : nom.hashCode());
                        result = prime
                                        * result
                                        + ((vehiculePossede == null) ? 0 : vehiculePossede
                                                        .hashCode());
                        return result;
                }

                @Override
                public boolean equals(Object obj) {
                        if (this == obj)
                                return true;
                        if (obj == null)
                                return false;
                        if (getClass() != obj.getClass())
                                return false;
                        Vendeur other = (Vendeur) obj;
                        if (nom == null) {
                                if (other.nom != null)
                                        return false;
                        } else if (!nom.equals(other.nom))
                                return false;
                        if (vehiculePossede == null) {
                                if (other.vehiculePossede != null)
                                        return false;
                        } else if (!vehiculePossede.equals(other.vehiculePossede))
                                return false;
                        return true;
                }
         private int countObjects(Vehicule search, List<Vehicule> vehiculePossede2) {
                    int count = 0;
                    for (int i = 0; i < vehiculePossede2.size(); i++) {
                        if (vehiculePossede2.get(i).equals(search))
                            count++;
                    }
                    return count;
                }
         
                public void writeData(PrintWriter out)
                {
                        out.printf("%s|", this.nom);
                        List<Integer> T = new ArrayList<Integer>();
                        int numContained;
                        int good =0;
                        int j=0;
                        
                        for (int i = 0; i < this.vehiculePossede.size(); i++) {
                                
                            numContained = countObjects(this.vehiculePossede.get(i), vehiculePossede);
                            if(i==0)
                            {
                                T.add(vehiculePossede.get(i).getIdentifiant());
                                //System.out.println("modele = "+vehiculePossede.get(i).getIdentifiant()+" nb = "+numContained+" i="+i );
                                out.printf("%d|%d",vehiculePossede.get(i).getIdentifiant(),numContained);
                                this.vehiculehm.put(vehiculePossede.get(i).getIdentifiant(), numContained);
                            }
                            else
                            {
                                    for(j=0;j<T.size();j++)
                                    {
                                        //System.out.println("T["+j+"] = "+T.get(j) );
                                        if((vehiculePossede.get(i).getIdentifiant())!=T.get(j))
                                        {
                                                 good++;
                                                 
                                        }
                                    }
                                    if(good==T.size())
                                    {
                                        //System.out.println("modele = "+vehiculePossede.get(i).getIdentifiant()+" nb = "+numContained+" i="+i );
                                        out.printf("|%d|%d",vehiculePossede.get(i).getIdentifiant(),numContained);
                                        T.add( vehiculePossede.get(i).getIdentifiant());
                                        this.vehiculehm.put(vehiculePossede.get(i).getIdentifiant(), numContained);
                                    }
                            }
                            good=0;
                           
                        }
                        
                
                                        out.println();
                                        
                }

                public HashMap getVehiculehm() {
                        Set set = this.vehiculehm.entrySet(); 
                        // Get an iterator 
                        Iterator i = set.iterator(); 
                        // Display elements 
                        while(i.hasNext()) { 
                        Map.Entry me = (Map.Entry)i.next(); 
                        /*System.out.print(me.getKey() + ": "); 
                        System.out.println(me.getValue()); */
                        } 
                        System.out.println();
                
                        return vehiculehm;
                }

                public void setVehiculehm(HashMap vehiculehm) {
                        this.vehiculehm = vehiculehm;
                }
                

        
}
